#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"




void STInit(ST* ps)
{
	assert(ps);
	SDataType* ptr = (SDataType*)malloc(sizeof(SDataType) * 4);
	if (ptr == NULL)
	{
		perror("malloc fail");
		return;
	}
	ps->s = ptr;
	ps->capacity = 4;
	ps->top = 0;

}

void STDestroy(ST* ps)
{
	assert(ps);
	free(ps->s);
	ps->s = NULL;
	ps->capacity = 0;
	ps->top = 0;

}

void STPush(ST* ps, SDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		SDataType* tmp = (SDataType*)realloc(ps->s, sizeof(SDataType) * ps->capacity * 2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->s = tmp;
		ps->capacity *= 2;
	}
	ps->s[ps->top] = x;
	ps->top++;
	
}

bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

void STPop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));
	ps->top--;
}

int STSize(ST* ps)
{
	assert(ps);
	return ps->top - 1;

}

SDataType STTop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));
	return ps->s[ps->top - 1];
}